package com.oocl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.oocl.dao.UserDao;
import com.oocl.dao.impl.UserDaoImpl;
import com.oocl.pojo.User;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao userDao;
    public LoginServlet() {
    	userDao = new UserDaoImpl();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		System.out.println(name + " " + pwd);
		HttpSession sen = request.getSession();
		User u = userDao.checkUser(name, pwd);
		
		if(u != null){
			request.getSession().setAttribute("c", "success");
			request.getSession().setAttribute("username", u);
			request.getRequestDispatcher("sec/show").forward(request, response);
			Logger logger = Logger.getLogger(LoginServlet.class);
			logger.info(name + " log in...");
		}
		else{
			Logger logger = Logger.getLogger(LoginServlet.class);
			logger.info(name + " log in error...");
			request.getSession().setAttribute("error", "log in error");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
}
